package net.huashitong.user.service;

import com.sirdc.modules.core.dao.BaseDao;
import com.sirdc.modules.sys.base.StringPKService;
import com.sirdc.modules.sys.service.SysTableService;
import net.huashitong.user.dao.DepartmentDao;
import net.huashitong.user.entity.Department;
import net.huashitong.user.filter.DepartmentFilter;
import net.huashitong.utils.BeanSetGetUtil;
import net.huashitong.utils.CompareUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Service
public class DepartmentService extends StringPKService<Department> {
    @Autowired
    private DepartmentDao dao;
    @Autowired
    private SysTableService tableService;

    @Override
    protected BaseDao<Department, String> getDao() {
        return dao;
    }

    @Override
    protected void beforeSave(Department obj) {
        obj.setSysId(tableService.updateMaxSysId("t_bnp_department", null));
    }

    //查询分页
    public List<Department> queryByFilter(DepartmentFilter filter) {
        return dao.queryByFilter(filter);
    }

    //查询无分页
    public List<Department> queryByFilterNoPage(DepartmentFilter filter) {
        return dao.queryByFilterNoPage(filter);
    }

    //添加
    public void addDepartment(DepartmentFilter filter) {
        Department department = new Department();
        BeanUtils.copyProperties(filter, department);
        department = dealWithTypeName(department);
        this.save(department);
    }

    //修改
    public void updateDepartment(Department oldDept, Department newDept) throws NoSuchFieldException {
        //**********修改方法*********
        String[] arrays = {
                "updUser",
                "updTime",
                "updDate",
                "createUser",
                "createTime",
                "createDate"
        };
        newDept = dealWithTypeName(newDept);
        Map<String, List<Object>> map = CompareUtil.compareFields(oldDept, newDept, arrays);
        Iterator<Map.Entry<String, List<Object>>> entries = map.entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry<String, List<Object>> entry = entries.next();
            String key = entry.getKey();
            List<Object> value = entry.getValue();
            BeanSetGetUtil.setValue(oldDept,
                    oldDept.getClass(),
                    key,
                    Department.class.getDeclaredField(key).getType(),
                    value.get(1));
        }
        //**********修改方法*********
        this.update(oldDept);
    }

    /**
     * 查询单位名称是否已存在
     * @param deptName
     * @return
     */
    public boolean queryDeptNameExist(String deptName) {
        List<Department> list = dao.queryDeptNameExist(deptName);
        if (list.size() > 0) {
            return true;
        } else {
            return false;
        }
    }

    public Department dealWithTypeName(Department department){
        String deptType = department.getDeptType();
        switch (deptType) {
            case "0":
                department.setTypeName("业主单位");
                break;
            case "1":
                department.setTypeName("审批单位");
                break;
            case "4":
                department.setTypeName("督查组");
                break;
            case "2":
                department.setTypeName("其他");
                break;
            default:
                break;
        }
        return department;
    }

}
